package Yangtz.Cs.Manager.Controller;

import Yangtz.Cs.Entity.ReaderBean;
import Yangtz.Cs.Service.ReaderService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Reader;

@WebServlet("/LoginManagerServlet")
public class LoginManagerServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //统一浏览器以及服务器编码解码格式
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");

        //获取输入的参数
        String rdID = request.getParameter("rdID").trim();
        String rdPwd = request.getParameter("rdPwd").trim();

        //创建readerservice对象
        ReaderService readerService = new ReaderService();
        //service调用dao层方法，获取的参数值传递给FindReader
        ReaderBean reader = readerService.isManager(rdID, rdPwd);
//        判断reader是否存在，若1则跳转管理页面，否则提示用户名或密码错误
        if (reader == null) {//登陆成功，跳转页面
            response.sendRedirect("Login.jsp?error=yes");
            return;
        }

        HttpSession session=request.getSession();
        //设置到了session对象属性中
        session.setAttribute("manager",reader);

        System.out.println(session.getId());
        response.sendRedirect("Manager.jsp");
    }


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
